//首先直接获取search
let search = window.location.search;
if (search) {
    setEnv(search);
} else {
    search = window.location.href.slice(window.location.href.indexOf("?"));
    // console.log(search);
    setEnv(search);
    // window.environment = "production";
}

function setEnv(search) {
    let exits = false;
    for (let item of search.slice(1).split("&")) {
        if (item.split("=")[0] === "env") {
            // console.log("-0-0-0-0-0-0-0-0-，设置env为edit")
            window.environment = "edit";
            setParentPageCookie();
            exits = true;
            break;
        }
    }
    if (!exits) {
        window.environment = "production";
    }
}

//从父页面获取sessionId，并设置到当前域名下
function setParentPageCookie() {
    console.log("setParentPageCookie")
    window.addEventListener("message", receiveParentMessage, false);
    function receiveParentMessage(event) {
        try {
            let data = JSON.parse(event.data);
            console.log("接收到了父窗口的data", data);
            //设置sessionId
            setCookie("sessionId", data.sessionId);
            //设置企业编码
            sessionStorage.setItem("mg-enterpriseCode", data["mg-enterpriseCode"]);
        } catch (err) {
            console.log(err);
        }
        

        //初始化回调父窗口隐藏侧边栏方法的监听
        PSEvent.listen("hideMenu", function () {
            event.source.postMessage("hideMenu", event.origin);
        });
        //解除对该事件的监听
        window.removeEventListener("message", receiveParentMessage, false);
        console.log("解除对该事件的监听");
    }
    
}




